package cn.skcks.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

// sqlSessionFactory => sqlSession
public class MybatisUtils {

	private static SqlSessionFactory sqlSessionFactory;

	static {
		try {
			// 获取 SqlSessionFactory 对象
			String resource = "mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 从 SqlSessionFactory 获取 SqlSession 实例
	// SqlSession 完全包含了面向数据库执行 SQL 命令所需的所有方法
	public static SqlSession getSqlSession(){
		return sqlSessionFactory.openSession();
	}
}